package com.example.utils;

import com.example.entity.SysRight;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName TreeUtils
 * @description: TODO
 */
public class TreeViewUtils {


    public static List<TreeViewVo> formatTreeView(List<SysRight> dataSource) {

        List<TreeViewVo> resultList = dataSource.stream().map(sysRight -> {
            TreeViewVo treeViewVo = new TreeViewVo();
            treeViewVo.setId(sysRight.getRightCode());
            treeViewVo.setText(sysRight.getRightText());
            treeViewVo.setParentId(sysRight.getRightParentCode());
            return treeViewVo;
        }).collect(Collectors.toList());

        resultList= getChildList(resultList,"ROOT_MENU");
        return resultList;
    }


    private static List<TreeViewVo> getChildList(List<TreeViewVo> parentList, String parentCode) {
        List<TreeViewVo> chilrdens = parentList.stream()
                .filter(treeViewVo -> treeViewVo.getParentId().equals(parentCode))
                .map(treeViewVo -> {
                    treeViewVo.setNodes(getChildList(parentList, treeViewVo.getId()));
                    if(treeViewVo.getNodes().isEmpty()){
                        treeViewVo.setNodes(null);
                    }
                    return treeViewVo;
                }).collect(Collectors.toList());
        return chilrdens;
    }


}
